package com.uhu.code;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 3. 无重复字符的最长子串
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/?envType=study-plan-v2&envId=top-100-liked
 */
public class Solution3 {
    public int lengthOfLongestSubstring(String s) {
        if(s.length() == 0) {
            return 0;
        }
        char[] chars = s.toCharArray();
        int maxLength = 1;
        for(int i = 0; i < chars.length; i++) {
            int j = i + 1;
            List<String> tempList = new ArrayList<>();
            tempList.add(chars[i] + "");
            while(j < chars.length) {
                if(tempList.contains(chars[j] + "")) {
                    maxLength = maxInt(maxLength, tempList.size());
                    break;
                }else {
                    tempList.add(chars[j] + "");
                    j++;
                }
            }
            maxLength = maxInt(maxLength, tempList.size());
        }
        return maxLength;
    }

    private int maxInt(int a, int b) {
        return a>b?a:b;
    }
}
